﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Mvc;

namespace MyWeb.Common.Authorize
{
    public class LoginAuthorize : AuthorizeAttribute
    {
        private bool localAllowed;
        public LoginAuthorize(bool allowedParam=true)
        {
            localAllowed = allowedParam;
        }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (localAllowed)
            {
                //判断Cookies中是否有登录信息
                HttpCookie logincookies = httpContext.Request.Cookies.Get("UserID");

                if (string.IsNullOrEmpty(logincookies.Value))
                {
                    httpContext.Response.Write("<script>top.location.href = '~/Home/Login';</script>");
                }
            }
            return true;
        }
    }
}